using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameFrameWork;

/// <summary>
/// 远程攻击策略
/// </summary>
public class RangedAttackStrategy : AttackStrategyBase, IRangedAttack
{
    public BulletType ProjectileType => AttackData?.BulletData?.BulletType ?? BulletType.Liner;

    public RangedAttackStrategy(SkillData data)
    {
        AttackData = data;
    }

    public void LaunchProjectile(Vector3 targetPos)
    {
        if (Owner == null || AttackData == null || AttackData.BulletData == null)
        {
            Logger.LogError("RangedAttackStrategy", "无法发射子弹：缺少必要组件");
            return;
        }

        var bulletData = AttackData.BulletData;
        var bullet = BulletPool.Instance.GetBullet(bulletData);
        
        if (bullet == null)
        {
            Logger.LogError("RangedAttackStrategy", $"无法获取子弹：{bulletData.BulletType}");
            return;
        }

        // 设置子弹位置
        Vector3 startPos = Owner.transform.position;
        if (bulletData.OffVec3 != Vector3.zero)
        {
            startPos += bulletData.OffVec3;
        }
        bullet.transform.position = startPos;

        // 创建移动策略
        var movement = MoveStrategyFactory.Create(bulletData.MoveData);
        bullet.Initialize(owner: Owner, movement: movement);
    }

    public override void Execute(Entity target)
    {
        base.Execute(target);

        if (target == null)
        {
            Logger.LogWarning("RangedAttackStrategy", $"{Owner?.name} 攻击目标无效");
            return;
        }

        if (AttackData == null || AttackData.BulletData == null)
        {
            Logger.LogError("RangedAttackStrategy", $"{Owner?.name} 缺少子弹数据");
            return;
        }

        // 发射子弹
        Vector3 targetPos = target.transform.position;
        LaunchProjectile(targetPos);
        
        Logger.Log("RangedAttackStrategy", $"{Owner?.name} 向 {target.name} 发射远程攻击");
    }

    public override bool CheckCondition()
    {
        return base.CheckCondition() && 
               AttackData != null && 
               AttackData.BulletData != null;
    }
}
